import { HttpClient, HttpHeaders } from '@angular/common/http';
import { retry } from 'rxjs/operators';

declare var base: any;
declare var security: any;

/**
 * 定义GIS数据访问辅助类
 */
export class GisDataUtils {

    /**
     * 从后台获取当前地图方案的图层数据
     * 目前只支持fm地图方案数据
     */
    static getMap(http: HttpClient, success: any, fail: any) {
        const jwts = security.SecurityService.jwt;
        const url = base.BaseService.getHostUrl() + '/gismapstyledirectory/recommend';
        const obsData = http.post<any>(url, '', { headers: new HttpHeaders({ jwt: jwts }) })
            .pipe(retry(1));
        obsData.subscribe(
            (data) => {
                if (success) {
                    success(data);
                }
            },
            () => {
                if (fail) {
                    fail();
                }
            }
        );
    }

    /**
     * 从后台获取当前地图方案的图层数据
     * 目前只支持fm地图方案数据
     */
    static getLayers(http: HttpClient, solution: string, success: any, fail: any) {
        const jwts = security.SecurityService.jwt;
        const para = { browser: '1', terminal: '1', memory: 1024, kernel: 'mapbox' };
        const url = base.BaseService.getHostUrl() + '/styles/layers/' + solution;
        const obsData = http.post<any>(url, para, { headers: new HttpHeaders({ jwt: jwts }) })
            .pipe(retry(1));
        obsData.subscribe(
            (data) => {
                if (success) {
                    success(data);
                }
            },
            () => {
                if (fail) {
                    fail();
                }
            }
        );
    }

    /**
     * gis叠加分析
     */
    static analysis(http: HttpClient, oa: any, success?: any, fail?: any) {
        const jwts = security.SecurityService.jwt;
        const url = base.BaseService.getHostUrl() + '/analysisservice/analysis';
        const obsData = http.post<any>(url, oa.gas, { headers: new HttpHeaders({ jwt: jwts }) })
            .pipe(retry(1));
        obsData.subscribe(
            (data) => {
                if (!data) {
                    data = [];
                }
                oa.handlePromiseResult(data, success, fail);
            },
            (error: any) => {
                oa.isLoading = false;
                if (fail) {
                    fail(this);
                } else {
                    throw new Error(error);
                }
            });
    }

}
